package com.dongfa.learn_1101;

import java.util.Random;

public class LearnCode1103 {
    public static void main(String[] args) {
        LearnCode1103 learnCode1103 = new LearnCode1103();
        int[] arr = {1, 2, 3, 4, 5};
        learnCode1103.getMaxArr(arr);
        learnCode1103.getCount();
        learnCode1103.changeArr(arr);
        learnCode1103.mixArr(arr);
    }

    //获取数组中的最大值
    public void getMaxArr(int[] arr) {
        int temp = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > temp) {
                temp = arr[i];
            }
        }
        System.out.println("该数组最大值为：" + temp);
    }

    //生成10个1~100以内的随机数存入数组，求有多少个数字大于平均值
    public void getCount(){
        int[] arr = new int[10];
        int count = 0;
        int avg = 0;
        //存入十个数字
        for (int i = 0; i < 10; i++){
            Random r = new Random();
            arr[i] = r.nextInt(1, 101);
            avg += arr[i];
        }
        //计算出平均值
        avg /= arr.length;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > avg){
                count++;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println("平均值为：" + avg);
        System.out.println("大于平均数的个数为：" + count);
    }
    //交换数组中的数据
    public void changeArr(int[] arr){
        int temp = 0;
        for (int i = 0, j = arr.length -1; i != j; i++, j--){
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    //打乱数组
    public void mixArr(int[] arr){
        int temp = 0;
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int randomArrIndex = r.nextInt(0, arr.length);
            temp = arr[i];
            arr[i] = arr[randomArrIndex];
            arr[randomArrIndex] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println();
            System.out.print(arr[i] + " ");
        }
    }
}
